<?php

namespace App\Services\Payment\PayAdapter;


use App\Facades\PaymentConfigFacades;

class ZhongxinPayAdapter extends AbstractPayAdapter implements ZhongxinPayAdapterInterface
{
    protected $paymentService;

    public function __construct(){
        $this->paymentService = PaymentConfigFacades::getPaymentObject('zhongxinPay');
    }

    /**
     * 统一下单接口
     *
     * @param array $order
     * $order['body']               支付描述
     * $order['total_fee']          总金额(元)
     * $order['trade_type']         交易类型 JSAPI(公众号支付) NATIVE(扫码支付)
     * $order['openId']             用户openid 交易类型为JSAPI不能为空
     * $order['out_trade_no']       订单号（从生成账单接口获取）
     *
     * @return array
     */
    public function pay($order)
    {
        if(empty($order['body'])){
            return ["code"=>1,"msg"=>'支付描述不能为空'];
        }
        if(empty($order['total_fee'])){
            return ["code"=>1,"msg"=>'总金额不能为空'];
        }
        if(empty($order['product_id'])){
            return ["code"=>1,"msg"=>'商品id不能为空'];
        }
        if(empty($order['trade_type'])){
            return ["code"=>1,"msg"=>'交易类型不能为空'];
        }

        if(empty($order['out_trade_no'])){
            return ["code"=>1,"msg"=>'流水号不能为空'];
        }

        $data['trade_type'] = $order['trade_type'];
        $data['notifyUrl'] = $order['notify_url'];
        $data['total_fee'] = $order['total_fee']*100;
        $data['body'] = $order['body'];
        $data['out_trade_no'] = $order['out_trade_no'];
        $data['attach'] = $order['attach'];

        if(config('payment.zhongxinPay.mchId') != '7551000001'){
            if($order['trade_type']=='JSAPI'){
                if(!empty($order['openId'])){
                    $data['sub_openid']=$order['openId'];
                    $data['sub_appid']=config('payment.zhongxinPay.appid');
                }else{
                    return ["code"=>1,"msg"=>'openId不能为空'];
                }
            }
        }

        $payment = $this->paymentService->pay($data);
        $payment['sequenceNo']=$data['out_trade_no'];
        return $payment;
    }

    /**
     * 退款接口
     *
     * @param array $order
     * $order['buyerId']            用户id
     * $order['appCat']            店铺类型
     * $order["transaction_id"]     中信微信回调返回的交易号
     * $order["total_fee"]          订单总额
     * $order['out_trade_no']       原订单流水号
     * $order["refund_fee"]         退款金额
     * $order["refund_no"]          退款订单号
     * $order["goodsId"]            退款商品id
     *
     * @return array
     */
    public function refund($order)
    {
        if(empty($order['appCat'])){
            return ["code"=>1,"msg"=>'店铺类型不能为空'];
        }
        if(empty($order['buyerId'])){
            return ["code"=>1,"msg"=>'用户id不能为空'];
        }
        if(empty($order['total_fee'])){
            return ["code"=>1,"msg"=>'总金额不能为空'];
        }
        if(empty($order['refund_fee'])){
            return ["code"=>1,"msg"=>'退款金额不能为空'];
        }
        if(empty($order['refund_no'])){
            return ["code"=>1,"msg"=>'退款订单号不能为空'];
        }
        if(empty($order['goodsId'])){
            return ["code"=>1,"msg"=>'退款商品id不能为空'];
        }

        $data['out_trade_no'] = empty($order['out_trade_no'])?'':$order['out_trade_no'];                  //原订单号
        $data['transaction_id'] = empty($order['transaction_id'])?'':$order['transaction_id'];                  //平台订单号
        $data['out_refund_no'] = $order['refund_no'];                 //商户退款单号
        $data['total_fee'] = $order['total_fee']*100;                  //总金额
        $data['refund_fee'] = $order['refund_fee']*100;          //退款金额

        try{
            $result = $this->paymentService->refund($data);
            if($result['code']==0){


            }else{
                throw new \Exception($result['msg']);
            }
        }catch (\Exception $e){
            return ['code'=>1,'msg'=>$e->getMessage(),'trace'=>$e->getTraceAsString()];
        }
    }

    /**
     * 中信支付状态查询接口
     * @param array $order
     * @return array
     */
    public function zhongXinCheckPay($order)
    {
        if(empty($order['transaction_id']) && empty($order['out_trade_no'])){
            return ["code"=>1,'msg'=>'请输入商户订单号或平台订单号!'];
        }

        return $this->paymentService->checkPay($order);
    }

    /**
     * 中信退款状态查询接口
     * @param array $order
     * @return array
     */
    public function zhongXinCheckRefund($order)
    {
        if(empty($order['transaction_id']) && empty($order['out_trade_no']) && empty($order['out_refund_no']) && empty($order['refund_id'])){
            return ["code"=>1,'msg'=>'请输入商户订单号、平台订单号、商户退款单号或平台退款单号!'];
        }

        return $this->paymentService->checkRefund($order);
    }

    /**
     * 中信关闭订单接口
     * @param array $order
     * @return array
     */
    public function zhongXinCloseOrder($order)
    {
        if(empty($order['transaction_id']) && empty($order['out_trade_no'])){
            return ["code"=>1,'msg'=>'请输入商户订单号或平台订单号!'];
        }

        return $this->paymentService->closeOrder($order);
    }

    /**
     * 中信对账单下载接口
     * @param array $order
     * @return array
     */
    public function zhongXinPayBill($order)
    {
        return $this->paymentService->payBill($order);
    }
}
